const { DataTypes } = require("sequelize");

const seq = require("../db/seq");

// 创建模型（Model user -> users）
const User = seq.define(
  "user",
  {
    // id会被sequelize自动创建
    user_name: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      comment: "用户名，唯一",
    },
    nick_name: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "昵称",
    },
    is_admin: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: 0,
      comment: "是否为管理员；0否（默认）、1是",
    },
    password: {
      type: DataTypes.CHAR(64),
      allowNull: false,
      comment: "密码",
    },
  },
  {
    timestamps: true,
    createdAt: "createdAt",
    updatedAt: "updatedAt",
  }
);

// 初始化模型
async function initModel() {
  await User.sync({ alter: true });
  const res = await User.findOne({ where: { user_name: "admin" } });
  if (!res || !res.dataValues)
    await User.create({
      user_name: "admin",
      nick_name: "管理员",
      is_admin: 1,
      password: "$2a$10$810FTW6RZOD8Uy79nMaNu.xEq5tmM5uv.WDnlasauUugtPOpgUFva", // 123456
    });
}

// initModel();

module.exports = User;
